//
//  APPLENetworkTools.swift
//  123
//
//  Created by pangjiaxing on 15/12/30.
//  Copyright © 2015年 pangjiaxing. All rights reserved.
//

import UIKit
import AFNetworking

enum APPLERequestMethod:String{
    case GET="GET"
    case POST="POST"
}
class APPLENetworkTools: AFHTTPSessionManager {
    
    static let sharedTools:APPLENetworkTools={
        let instance=APPLENetworkTools()
        instance.responseSerializer.acceptableContentTypes?.insert("text/html")
        instance.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return instance
    }()

    typealias APPLERequestCallBack=(response:AnyObject?,error:NSError?)->()

    func request(method:APPLERequestMethod = .GET,urlstring:String,paraneter:AnyObject?,finished:APPLERequestCallBack){
        
        let success = {(dataTasks:NSURLSessionDataTask?,responseObject:AnyObject?)->() in
            finished(response: responseObject, error: nil)
        }
        let failure = {(dataTasks:NSURLSessionDataTask?,error:NSError)->() in
            finished(response: nil, error: error)
        }
        
        if method == .GET{
            GET(urlstring, parameters: paraneter, progress: nil , success: success, failure: failure)
        }else{
            POST(urlstring, parameters: paraneter, progress: nil, success: success, failure: failure)
        }
    }
}

//首页数据请求
extension APPLENetworkTools {
    func loadStatuses(accessToken:String,maxId:Int64,sinceId:Int64,finished:APPLERequestCallBack){
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token": APPLEUserAccountViewModel.sharedViewModel.accessToken!,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        request(urlstring:urlString , paraneter: params, finished: finished)
    }
}
//OAuth 登陆获得数据
extension APPLENetworkTools{
    func loadAccessToken(code: String, finished:APPLERequestCallBack){
        // 请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": WB_APPKEY,
            "client_secret": WB_SECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WB_REGIRECTURI
        ]
        // 发送请求
        request(.POST, urlstring: urlString, paraneter: params, finished: finished)
    }
    
    /// 请求个人信息
    ///
    /// - parameter accessToken: <#accessToken description#>
    /// - parameter uid:         <#uid description#>
    func loadUserAccount(accessToken: String, uid: String, finished: APPLERequestCallBack){
        // 请求地址
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请参数
        let params = [
            "access_token": accessToken,
            "uid": uid
        ]
        request(urlstring: urlString, paraneter: params, finished: finished)
    }

}
// MARK: - 发送微博数据
extension APPLENetworkTools {
    
    
    /// 发送文字微博的
    func update(accessToken: String, status: String, finished: APPLERequestCallBack){
        
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": accessToken,
            "status": status
        ]
        
        request(.POST, urlstring: url, paraneter: params, finished: finished)
    }
    
}
